<?php 
if(!isset($_SESSION)) 
	{ 
		session_start(); 
	}
$lang = $_SESSION['lang'];
if (empty($lang) || $lang == "EN") {
	include "../../value/value_en.php";
} else if($lang == "VN") {
	include "../../value/value_vn.php";
}
else {
	include "../../value/value_kr.php";
}
include '../../generallibraries/funcs.php';
include '../../operations/connection.php';	

$list["ac_customer.Email"]="ASC";
$list["ac_customer.CustomerName"]="ASC";
$list["ac_customer.Address"]="ASC";
$list["ac_customer.Phone"]="ASC";
$list["ac_customer.Fax"]="ASC";
$list["ac_customer.CreatedDate"]="DESC";
$str="ac_customer.CreatedDate";
$page=1;
$co=0;
if(isset($_REQUEST['proc'])){
	if($_REQUEST['proc']=="add"){
		if($_SESSION['checknamecus']=="okk"){
			$Description=$_REQUEST["txtdes"];
			$Mobile=$_REQUEST["txtmobile"];
			$Phone=$_REQUEST["txtphone"];
			$Fax=$_REQUEST["txtfax"];
			$CustomerName=$_REQUEST["txtcomname"];
			$Address=$_REQUEST["txtaddress"];
			$Website=$_REQUEST["txtwebsite"];
			$CompanyType1=$_REQUEST["txtcomtype121"];
			$CompanyType2=$_REQUEST["txtcomtype122"];
			$Agent=$_REQUEST["txtcontactperson"];
			$Email=$_REQUEST["txtmail"];
			$IDNumber=$_REQUEST["txtcomnum"];
			$TaxID = $_REQUEST["txttax"];
			$CompanyType3 =$_REQUEST["txttype"];
			$Activest =$_REQUEST["txtactive"];
			$CreatedDate = $_REQUEST["txttrandate"];
			$a= (string)(microtime(true)*1000);
			$b=split('\.',$a);
			$CustomerID="KHTT".$b[0];
			$sql="insert into ac_customer (CustomerID,CustomerName,Agent,IDNumber,Phone,Website,Description,Fax,Address,Email,Mobile,CompanyType1,CompanyType2,CompanyType3,TaxID,CreatedDate,Activest) values ('".$CustomerID."','".$CustomerName."'
			,'".$Agent."','".$IDNumber."','".$Phone."','".$Website."','".$Description."','".$Fax."',
			'".$Address."','".$Email."','".$Mobile."','".$CompanyType1."','".$CompanyType2."','".$CompanyType3."'
			,".$TaxID.",'".$CreatedDate."',".$Activest.")";
			$rs=mysql_query($sql);
			if($rs>0){
				$_SESSION['ketquathucthi']="l_themthanhcong";
			}else{
				$_SESSION['ketquathucthi']="l_themkhongthanhcong";
			}
		}
	}else if($_REQUEST['proc']=="update"){
		if(isset($_GET['page']))
			$page=$_GET['page'];
		else $page=1;
		$list=$_SESSION['accsort'];
		$str=$_SESSION['accsortn'];
		if($_SESSION['checknamecus']=="okk"){
			$Description=$_REQUEST["txtdes"];
			$Mobile=$_REQUEST["txtmobile"];
			$Phone=$_REQUEST["txtphone"];
			$Fax=$_REQUEST["txtfax"];
			$CustomerName=$_REQUEST["txtcomname"];
			$Address=$_REQUEST["txtaddress"];
			$Website=$_REQUEST["txtwebsite"];
			$CompanyType1=$_REQUEST["txtcomtype121"];
			$CompanyType2=$_REQUEST["txtcomtype122"];
			$Agent=$_REQUEST["txtcontactperson"];
			$Email=$_REQUEST["txtmail"];
			$IDNumber=$_REQUEST["txtcomnum"];
			$TaxID = $_REQUEST["txttax"];
			$CompanyType3 =$_REQUEST["txttype"];
			$Activest =$_REQUEST["txtactive"];
			$CreatedDate = $_REQUEST["txttrandate"];
			$CustomerID=$_REQUEST["idcus"];
			if(isset($CustomerID)){
				$CustomerID=$_REQUEST["idcus"];
				$sql="update ac_customer set ac_customer.CustomerName='".$CustomerName."',
				ac_customer.Agent='".$Agent."',
				ac_customer.IDNumber='".$IDNumber."',
				ac_customer.Phone='".$Phone."',
				ac_customer.Website='".$Website."',
				ac_customer.Description='".$Description."',
				ac_customer.Fax='".$Fax."',
				ac_customer.Address='".$Address."',
				ac_customer.Email='".$Email."',
				ac_customer.Mobile='".$Mobile."',
				ac_customer.CompanyType1='".$CompanyType1."',
				ac_customer.CompanyType2='".$CompanyType2."',
				ac_customer.CompanyType3='".$CompanyType3."',
				ac_customer.TaxID=".$TaxID.",
				ac_customer.CreatedDate='".$CreatedDate."',
				ac_customer.Activest=".$Activest." where ac_customer.CustomerID='".$CustomerID."'";
				$rs=mysql_query($sql);
				if($rs>0){
					$_SESSION['ketquathucthi']="l_capnhatthanhcong";
				}else{
					$_SESSION['ketquathucthi']="l_capnhatkhongthanhcong";
				}
			}
		}
	}else if($_REQUEST['proc']=="delete"){
		if(isset($_GET['page']))
			$page=$_GET['page'];
		else $page=1;
		$list=$_SESSION['accsort'];
		$str=$_SESSION['accsortn'];
		$CustomerID=$_REQUEST["idcus"];
		if(isset($CustomerID)){
			$sql1="select Activest from ac_customer where ac_customer.CustomerID='".$CustomerID."'";
			$tt=mysql_query($sql1);
			$row=mysql_fetch_array($tt);
			if($row[Activest]==0){
				$sql="delete from ac_customer where ac_customer.CustomerID='".$CustomerID."'and ac_customer.Activest=0 ";
				$rs=mysql_query($sql);
				if($rs>0){
					$_SESSION['ketquathucthi']="l_xoathanhcong";
				}else{
					$_SESSION['ketquathucthi']="l_xoakhongthanhcong";
				}
			}else{
				$_SESSION['ketquathucthi']="l_customeractivenotdelete";
			}
			
		}
	}
}
$_SESSION['accsortn']=$str;
$_SESSION['accsort']=$list;
$rows_per_page = $_SESSION['accrows_page'] ;
$page_start = ( $page - 1 ) * $rows_per_page; 
$page_end = $page * $rows_per_page;
$hamsort=" ORDER BY ".$str." ".$list[$str]."";
$sqlCustomer = generalFunctions::listCustomer("ac_customer.CustomerID, ac_customer.CustomerName, ac_customer.Address, ac_customer.Phone, ac_customer.Fax,ac_customer.Email",$hamsort);
$mangCustomer=mysql_query($sqlCustomer);
$number_of_page = ceil ( mysql_num_rows( $mangCustomer ) / $rows_per_page ); 
$_SESSION['checksoluong']=mysql_num_rows( $mangCustomer );
$_POST['searchname']="";
?>
<input type="hidden" name="content" value="accountant-setup-customer-content" />
	<div style="margin: 10px 0px 5px 5px;">
		<table>
			<tr>
				<td style="vertical-align:middle"><?php echo $l_customername;?></td>
				<td id="txtsearch"><input onkeyup="changeSearch(this.value);" style="width:250px; autocomplete:off; " type="text" name="searchname" class="in_testfield" id="searchname"  value="<?=$_POST['searchname']?>"/></td>
				<td><input onclick="resetcustomer();" type="button" value="Reset" class="btnupbg btnupload" /></td>
			</tr>
		</table>
	</div>
	<div id="viewlistcustomer" style="width:100%">
	<table class="cssTable" width="100%" >
		<thead>
			<tr>
				<th scope="col"></th>
				<th scope="col"><a onclick="sort(<?=$page?>,'ac_customer.CustomerName',1);" ><?php echo $l_customername;?></a></th>
				<th scope="col"><a onclick="sort(<?=$page?>,'ac_customer.Address',1);" ><?php echo $l_address;?></a></th>
				<th scope="col"><a onclick="sort(<?=$page?>,'ac_customer.Phone',1);" ><?php echo $l_phone;?></a></th>
				<th scope="col"><a onclick="sort(<?=$page?>,'ac_customer.Fax',1);" ><?php echo $l_Fax;?></a></th>
				<th scope="col"><a onclick="sort(<?=$page?>,'ac_customer.Email',1);" ><?php echo $l_email;?></a></th>
			</tr>
		</thead>
		<tbody>
			<?php 
				if(mysql_num_rows($mangCustomer)>0){
					$i=0;
					while($row = mysql_fetch_array($mangCustomer)){
						if ( $i >= $page_start ){
						?>
							<tr>
							<td style="width: 10px">
								<div style="text-align:center">
									<input onclick="checkonly(this.id,document.accsetupcus.list,<?=$page?>);" type="checkbox" name="list" id="<?php echo 'checkcus'.$i?>" value="<?=$row[CustomerID]?>" 
										<?php 
											if(isset($_REQUEST["idcus"])){
												if($row[CustomerID]==$_REQUEST["idcus"]){
													?>
													checked="checked"
													<?php 
												}
											}
										?>
									/>
								</div>
							</td>
							<td style="width: 150px"><?php echo $row[CustomerName];?></td>
							<td><?php echo $row[Address];?></td>
							<td style="width: 80px"><?php echo $row[Phone];?></td>
							<td style="width: 80px"><?php echo $row[Fax];?></td>
							<td style="width: 80px"><?php echo $row[Email];?></td>
							</tr>
						<?php 
						}
						$i++;
						if ($i >= $page_end){ 
							break; 
						}
					}								
				}
			?>
			<tr>
				<td colspan="6" class="bt_td_main">
					<span style="background:#fff; padding:1px; display: inline-block;">
												
						<?php if ( $number_of_page > 1 ) 
						{ 
							if ($page==1)
								$p=2;
							else if ($page==$number_of_page)
								$p=$number_of_page-1;
							else $p=$page;
							if($page!=1){?>
								<a onclick="sort(1,'<?=$str?>',0);" ><span class='button_next'><?php echo "<<<";?></span> </a>
								<a onclick="sort(<?=($page-1)?>,'<?=$str?>',0);" ><span class='button_next'><?php echo "<<";?></span> </a>
							<?php  }
							else{ 
								?>
								<span class='button_next'><?php echo "<<<";?></span>
								<span class='button_next'><?php echo "<<";?></span>
								<?php
							}
							for ( $i = $p-1; $i <= $p+1; $i++ ) {	
									if ($i<=$number_of_page && $i>0)
									
										if ($i==$page){
											echo "<span class='button_next'><b>".$i."</b></span>"; 
										}else{
										?>
											<a onclick="sort(<?=$i?>,'<?=$str?>',0);" ><span class="button_next" style="color: #508e21;">
											<?php 
											echo $i;
											?>
											</span> </a>
											<?php 
										}
									?>
								
							<?php 
									}
								
								if($page!=$number_of_page){ ?>
									<a onclick="sort(<?=($page+1)?>,'<?=$str?>',0);"><span class='button_next'><?php echo ">>";?></span> </a>
									<a onclick="sort(<?=($number_of_page)?>,'<?=$str?>',0);" ><span class='button_next'><?php echo ">>>";?></span> </a>
								<?php }
								else{
									echo "<span class='button_next'>>></span> ";
									echo "<span class='button_next'>>>></span> ";
								}
							}
						
							
							?>
																				
					</span>
				</td>
			</tr>
		</tbody>
	</table>
	</div>
	<?php mysql_close($connection);?>